package com.javaweb.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.web.servlet.mvc.method.RequestMappingInfo;

import com.javaweb.base.po.Config;
import com.javaweb.base.po.Dictionary;
import com.javaweb.base.po.Interfaces;
import com.javaweb.constant.CommonConstant;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 系统内存
 * @author tank2140896
 */
public class BaseSystemMemory {
	
	/**
	 * 当前激活的配置文件参数信息（目前没有功能用到）
	 * @see com.javaweb.listener.ApplicationEnvironmentListener
	 */
	public static Map<String,Object> currentActivePropertyMap = new HashMap<>();
	
	/**
	 * 接口列表
     * <p>
     * 与interfacesMap相关联
	 */
	public static List<Interfaces> interfacesList = new ArrayList<>();
    
	/**
	 * 接口信息
     * <p>
     * 与interfacesList相关联
	 */
    public static Map<String,Interfaces> interfacesMap = new HashMap<>();
    
    /**
     * 字典列表
     * <p>
     * 与dictionaryMap相关联
     */
    public static List<Dictionary> dictionaryList = new ArrayList<>();
    
    /**
     * 字典信息
     * <p>
     * 与dictionaryList相关联
     */
    public static Map<String,Dictionary> dictionaryMap = new HashMap<>();
    
    /**
     * 配置列表
     * <p>
     * 与configMap相关联
     */
    public static List<Config> configList = new ArrayList<>();
    
    /**
     * 配置信息
     * <p>
     * 与configList相关联
     */
    public static Map<String,Config> configMap = new HashMap<>();
    
    /**
     * 接口请求映射信息
     */
    public static Map<String,RequestMappingInfo> requestMappingUrlMap = new HashMap<>();

    /**
     * 获取请求映射信息
     * @param httpServletRequest HttpServletRequest
     * @return Interfaces 接口信息
     */
    public static Interfaces getMatchRequestMapping(HttpServletRequest httpServletRequest) {
    	Set<String> set = requestMappingUrlMap.keySet();
    	for(String each:set) {
    		RequestMappingInfo requestMappingInfo = requestMappingUrlMap.get(each);
    		if(requestMappingInfo.getMatchingCondition(httpServletRequest)!=null) {
    			return interfacesMap.get(each);
    		}
    	}
    	return null;
    }
    
    /* -------------------------------------------------- dictionary -------------------------------------------------- */
    
    /**
     * 根据key值获取字典信息
     * @param key key值
     * @return Dictionary 字典信息
     */
    public static Dictionary getDictionaryByKey(String key) {
    	return dictionaryMap.get(key);
    }
    
    /**
     * 根据key值获取字典信息的value值
     * @param key key值
     * @return String 字典信息的value值
     */
    public static String getDictionaryValueByKey(String key) {
    	String value = CommonConstant.NULL_VALUE;
    	Dictionary dictionary = getDictionaryByKey(key);
    	if(dictionary!=null) {
    		value = dictionary.getValueCode();
    	}
    	return value;
    }
    
    /**
     * 根据key值获取字典信息的value值
     * @param key key值
     * @param defaultValue 默认值 
     * @return String 字典信息的value值
     */
    public static String getDictionaryValueByKey(String key,String defaultValue) {
    	String value = defaultValue;
    	Dictionary dictionary = getDictionaryByKey(key);
    	if(dictionary!=null) {
    		value = dictionary.getValueCode();
    	}
    	return value;
    }
    
    /* -------------------------------------------------- config -------------------------------------------------- */
    
    /**
     * 根据key值获取配置信息
     * @param key key值
     * @return Config 配置信息
     */
    public static Config getConfigByKey(String key) {
    	return configMap.get(key);
    }
    
    /**
     * 根据key值获取配置信息的value值
     * @param key key值
     * @return String 配置信息的value值
     */
    public static String getConfigValueByKey(String key) {
    	String value = CommonConstant.NULL_VALUE;
    	Config config = getConfigByKey(key);
    	if(config!=null){
    		value = config.getValueCode();
    	}
    	return value;
    }
    
    /**
     * 根据key值获取配置信息的value值
     * @param key key值
     * @param defaultValue 默认值 
     * @return String 配置信息的value值
     */
    public static String getConfigValueByKey(String key,String defaultValue) {
     	String value = defaultValue;
    	Config config = getConfigByKey(key);
    	if(config!=null) {
    		value = config.getValueCode();
    	}
    	return value;
    }
    
}
